package com.ljx.recruitment.user.service.impl.auth;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ljx.recruitment.common.constant.CacheConstants;
import com.ljx.recruitment.common.enums.CommonStatusTypeEnum;
import com.ljx.recruitment.common.exception.ApiException;
import com.ljx.recruitment.common.util.RedisUtil;
import com.ljx.recruitment.security.util.UserUtil;
import com.ljx.recruitment.user.domain.entity.User;
import com.ljx.recruitment.user.domain.vo.req.AuthReq;
import com.ljx.recruitment.user.mapper.UserMapper;
import com.ljx.recruitment.user.service.AuthService;
import org.springframework.util.DigestUtils;

import java.util.Objects;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-10-12  16:54
 */
public class AccountAuthServiceImpl implements AuthService {

    @Override
    public User tryToLogin(AuthReq authReq) {
        checkLoginParams(authReq);

        User user = SpringUtil.getBean(UserMapper.class).selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getUsername, authReq.getUsername())
                .eq(User::getStatus, CommonStatusTypeEnum.NORMAL.getKey())
                .eq(User::getTenantId, UserUtil.tryToGetTenantId()));
        boolean flag = Objects.isNull(user) || !user.getPassword().equals(DigestUtils.md5DigestAsHex(authReq.getPassword().getBytes()));
        if (flag) {
            throw new ApiException("用户名或者密码错误");
        }
        return user;
    }

    private void clearRegisterCodeCache(String requestId) {
        RedisUtil.del(CacheConstants.getKey(CacheConstants.REGISTER_EMAIL, requestId));
    }

    private void checkLoginParams(AuthReq authReq) {
        if (Objects.isNull(authReq) || StrUtil.isEmpty(authReq.getUsername()) || StrUtil.isEmpty(authReq.getPassword())) {
            throw new ApiException("参数不能为空");
        }
    }

    private void checkRegisterParams(AuthReq authReq) {
        if (Objects.isNull(authReq) || StrUtil.isEmpty(authReq.getUsername()) || StrUtil.isEmpty(authReq.getPassword())
                || StrUtil.isEmpty(authReq.getPassword2()) || StrUtil.isEmpty(authReq.getEmail())
                || StrUtil.isEmpty(authReq.getCode()) ) {
            throw new ApiException("参数不能为空");
        }
        if (!authReq.getPassword().equals(authReq.getPassword2())) {
            throw new ApiException("两次密码输入不正确");
        }
        String code = RedisUtil.get(CacheConstants.getKey(CacheConstants.REGISTER_EMAIL, authReq.getRequestId()));
        if (StrUtil.isEmpty(code)){
            throw new ApiException("验证码已过期");
        }
        if (!authReq.getCode().equals(code)){
            throw new ApiException("验证码不正确");
        }
    }

    @Override
    public User tryToRegister(AuthReq authReq) {
        checkRegisterParams(authReq);

        UserMapper userMapper = SpringUtil.getBean(UserMapper.class);
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getUsername, authReq.getUsername())
                .eq(User::getStatus, CommonStatusTypeEnum.NORMAL.getKey())
                .eq(User::getTenantId, UserUtil.tryToGetTenantId()));
        if (Objects.nonNull(user)) {
            throw new ApiException("用户已存在");
        }

        User defaultUser = new User(authReq.getUsername(), DigestUtils.md5DigestAsHex(authReq.getPassword().getBytes()), UserUtil.tryToGetTenantId());;
        userMapper.insert(defaultUser);

        clearRegisterCodeCache(authReq.getRequestId());

        return defaultUser;
    }
}
